﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using HslCommunication.BasicFramework;

namespace HslCommunication.MQTT
{
	/// <summary>
	/// Mqtt的会话信息，包含了一些基本的信息内容，客户端的IP地址及端口，Client ID，用户名，活动时间，是否允许发布数据等等<br />
	/// Mqtt's session information includes some basic information content, the client's IP address and port, Client ID, user name, activity time, whether it is allowed to publish data, etc.
	/// </summary>
	// Token: 0x020000C7 RID: 199
	public class MqttSession
	{
		/// <summary>
		/// 实例化一个对象，指定ip地址及端口，以及协议内容<br />
		/// Instantiate an object, specify ip address and port, and protocol content
		/// </summary>
		/// <param name="endPoint">远程客户端的IP地址</param>
		/// <param name="protocol">协议信息</param>
		// Token: 0x06000EB9 RID: 3769 RVA: 0x000573E0 File Offset: 0x000555E0
		public MqttSession(IPEndPoint endPoint, string protocol)
		{
			this.Topics = new List<string>();
			this.ActiveTime = DateTime.Now;
			this.OnlineTime = DateTime.Now;
			this.ActiveTimeSpan = TimeSpan.FromSeconds(1000000.0);
			this.EndPoint = endPoint;
			this.Protocol = protocol;
		}

		/// <summary>
		/// 远程的ip地址端口信息<br />
		/// Remote ip address port information
		/// </summary>
		// Token: 0x17000225 RID: 549
		// (get) Token: 0x06000EBA RID: 3770 RVA: 0x00057449 File Offset: 0x00055649
		// (set) Token: 0x06000EBB RID: 3771 RVA: 0x00057451 File Offset: 0x00055651
		public IPEndPoint EndPoint { get; set; }

		/// <summary>
		/// 当前接收的客户端ID信息<br />
		/// Client ID information currently received
		/// </summary>
		// Token: 0x17000226 RID: 550
		// (get) Token: 0x06000EBC RID: 3772 RVA: 0x0005745A File Offset: 0x0005565A
		// (set) Token: 0x06000EBD RID: 3773 RVA: 0x00057462 File Offset: 0x00055662
		public string ClientId { get; set; }

		/// <summary>
		/// 当前客户端的激活时间<br />
		/// The activation time of the current client
		/// </summary>
		// Token: 0x17000227 RID: 551
		// (get) Token: 0x06000EBE RID: 3774 RVA: 0x0005746B File Offset: 0x0005566B
		// (set) Token: 0x06000EBF RID: 3775 RVA: 0x00057473 File Offset: 0x00055673
		public DateTime ActiveTime { get; set; }

		/// <summary>
		/// 获取当前的客户端的上线时间<br />
		/// Get the online time of the current client
		/// </summary>
		// Token: 0x17000228 RID: 552
		// (get) Token: 0x06000EC0 RID: 3776 RVA: 0x0005747C File Offset: 0x0005567C
		// (set) Token: 0x06000EC1 RID: 3777 RVA: 0x00057484 File Offset: 0x00055684
		public DateTime OnlineTime { get; private set; }

		/// <summary>
		/// 两次活动的最小时间间隔<br />
		/// Minimum time interval between two activities
		/// </summary>
		// Token: 0x17000229 RID: 553
		// (get) Token: 0x06000EC2 RID: 3778 RVA: 0x0005748D File Offset: 0x0005568D
		// (set) Token: 0x06000EC3 RID: 3779 RVA: 0x00057495 File Offset: 0x00055695
		public TimeSpan ActiveTimeSpan { get; set; }

		/// <summary>
		/// 当前客户端绑定的套接字对象
		/// </summary>
		// Token: 0x1700022A RID: 554
		// (get) Token: 0x06000EC4 RID: 3780 RVA: 0x0005749E File Offset: 0x0005569E
		// (set) Token: 0x06000EC5 RID: 3781 RVA: 0x000574A6 File Offset: 0x000556A6
		internal Socket MqttSocket { get; set; }

		/// <summary>
		/// 当前客户端订阅的所有的Topic信息<br />
		/// All Topic information subscribed by the current client
		/// </summary>
		// Token: 0x1700022B RID: 555
		// (get) Token: 0x06000EC6 RID: 3782 RVA: 0x000574AF File Offset: 0x000556AF
		// (set) Token: 0x06000EC7 RID: 3783 RVA: 0x000574B7 File Offset: 0x000556B7
		private List<string> Topics { get; set; }

		/// <summary>
		/// 当前的用户名<br />
		/// Current username
		/// </summary>
		// Token: 0x1700022C RID: 556
		// (get) Token: 0x06000EC8 RID: 3784 RVA: 0x000574C0 File Offset: 0x000556C0
		// (set) Token: 0x06000EC9 RID: 3785 RVA: 0x000574C8 File Offset: 0x000556C8
		public string UserName { get; set; }

		/// <summary>
		/// 当前的协议信息，一般为 "MQTT"，如果是同步客户端那么是 "HUSL"，如果是文件客户端就是 "FILE"<br />
		/// The current protocol information, generally "MQTT", if it is a synchronous client then it is "HUSL", if it is a file client it is "FILE"
		/// </summary>
		// Token: 0x1700022D RID: 557
		// (get) Token: 0x06000ECA RID: 3786 RVA: 0x000574D1 File Offset: 0x000556D1
		// (set) Token: 0x06000ECB RID: 3787 RVA: 0x000574D9 File Offset: 0x000556D9
		public string Protocol { get; private set; }

		/// <summary>
		/// 获取或设置当前的MQTT客户端是否允许发布消息，默认为False，如果设置为True，就是禁止发布消息，服务器不会触发收到消息的事件。<br />
		/// Gets or sets whether the current MQTT client is allowed to publish messages, the default is False, 
		/// if set to True, it is forbidden to publish messages, The server does not trigger the event of receiving a message.
		/// </summary>
		// Token: 0x1700022E RID: 558
		// (get) Token: 0x06000ECC RID: 3788 RVA: 0x000574E2 File Offset: 0x000556E2
		// (set) Token: 0x06000ECD RID: 3789 RVA: 0x000574EA File Offset: 0x000556EA
		public bool ForbidPublishTopic { get; set; }

		/// <summary>
		/// 检查当前的会话对象里是否订阅了指定的主题内容<br />
		/// Check whether the specified topic content is subscribed in the current session object
		/// </summary>
		/// <param name="topic">主题信息</param>
		/// <returns>如果订阅了，返回 True, 否则，返回 False</returns>
		// Token: 0x06000ECE RID: 3790 RVA: 0x000574F4 File Offset: 0x000556F4
		public bool IsClientSubscribe(string topic)
		{
			bool result = false;
			object obj = this.objLock;
			lock (obj)
			{
				result = this.Topics.Contains(topic);
			}
			return result;
		}

		/// <summary>
		/// 获取当前客户端订阅的所有的Topic信息<br />
		/// Get all Topic information subscribed by the current client
		/// </summary>
		/// <returns>主题列表</returns>
		// Token: 0x06000ECF RID: 3791 RVA: 0x00057548 File Offset: 0x00055748
		public string[] GetTopics()
		{
			object obj = this.objLock;
			string[] result;
			lock (obj)
			{
				result = this.Topics.ToArray();
			}
			return result;
		}

		/// <summary>
		/// 当前的会话信息新增一个订阅的主题信息<br />
		/// The current session information adds a subscribed topic information
		/// </summary>
		/// <param name="topic">主题的信息</param>
		// Token: 0x06000ED0 RID: 3792 RVA: 0x00057598 File Offset: 0x00055798
		public void AddSubscribe(string topic)
		{
			object obj = this.objLock;
			lock (obj)
			{
				bool flag2 = !this.Topics.Contains(topic);
				if (flag2)
				{
					this.Topics.Add(topic);
				}
			}
		}

		/// <summary>
		/// 当前的会话信息新增多个订阅的主题信息<br />
		/// The current session information adds multiple subscribed topic information
		/// </summary>
		/// <param name="topics">主题的信息</param>
		// Token: 0x06000ED1 RID: 3793 RVA: 0x000575FC File Offset: 0x000557FC
		public void AddSubscribe(string[] topics)
		{
			bool flag = topics == null;
			if (!flag)
			{
				object obj = this.objLock;
				lock (obj)
				{
					for (int i = 0; i < topics.Length; i++)
					{
						bool flag3 = !this.Topics.Contains(topics[i]);
						if (flag3)
						{
							this.Topics.Add(topics[i]);
						}
					}
				}
			}
		}

		/// <summary>
		/// 移除会话信息的一个订阅的主题
		/// </summary>
		/// <param name="topic">主题</param>
		// Token: 0x06000ED2 RID: 3794 RVA: 0x00057684 File Offset: 0x00055884
		public void RemoveSubscribe(string topic)
		{
			object obj = this.objLock;
			lock (obj)
			{
				bool flag2 = this.Topics.Contains(topic);
				if (flag2)
				{
					this.Topics.Remove(topic);
				}
			}
		}

		/// <summary>
		/// 移除会话信息的一个订阅的主题<br />
		/// Remove a subscribed topic from session information
		/// </summary>
		/// <param name="topics">主题</param>
		// Token: 0x06000ED3 RID: 3795 RVA: 0x000576E4 File Offset: 0x000558E4
		public void RemoveSubscribe(string[] topics)
		{
			bool flag = topics == null;
			if (!flag)
			{
				object obj = this.objLock;
				lock (obj)
				{
					for (int i = 0; i < topics.Length; i++)
					{
						bool flag3 = this.Topics.Contains(topics[i]);
						if (flag3)
						{
							this.Topics.Remove(topics[i]);
						}
					}
				}
			}
		}

		/// <summary>
		/// 获取当前的会话信息，包含在线时间的信息<br />
		/// Get current session information, including online time information
		/// </summary>
		/// <returns>会话信息，包含在线时间</returns>
		// Token: 0x06000ED4 RID: 3796 RVA: 0x00057768 File Offset: 0x00055968
		public string GetSessionOnlineInfo()
		{
			StringBuilder stringBuilder = new StringBuilder(this.ToString());
			stringBuilder.Append(" [" + SoftBasic.GetTimeSpanDescription(DateTime.Now - this.OnlineTime) + "]");
			return stringBuilder.ToString();
		}

		/// <inheritdoc />
		// Token: 0x06000ED5 RID: 3797 RVA: 0x000577B8 File Offset: 0x000559B8
		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder(string.Format("{0} Session[IP:{1}]", this.Protocol, this.EndPoint));
			bool flag = !string.IsNullOrEmpty(this.ClientId);
			if (flag)
			{
				stringBuilder.Append(" [ID:" + this.ClientId + "]");
			}
			bool flag2 = !string.IsNullOrEmpty(this.UserName);
			if (flag2)
			{
				stringBuilder.Append(" [Name:" + this.UserName + "]");
			}
			return stringBuilder.ToString();
		}

		// Token: 0x04000411 RID: 1041
		private object objLock = new object();
	}
}
